var deliverType=0;
var yhj=0;
$(function () {
    var order=getRequest();
    var seatArray=order["seatArray"];
    var placeId=order["placeId"];
    var commdityId=order["commdityId"];
    var schedulerId=order["schedulerId"];
    var skuId=order["skuId"];
    var money=order["money"];
    var placeName=order["placeName"];
    var sessionTime=order["sessionTime"];
    var a=order["a"];
    findPsType(commdityId,placeName,sessionTime);
    findCoupon(1);
    charng();
    shiyong();
   $("#money").html(parseInt(money)-parseInt(yhj));
    var seatInfo=seatArray.split(",");
    var ha=0;
    for(var i=0;i<seatInfo.length;i++){
        ha++;
        var haa="";
        var hee="";
        var hei="";
        if(ha==1){
            haa="p3";
            hee="p4";
            hei="p5";
        }
        var a=seatInfo[i].split("_");
        $(".msg-2").append("<p class='"+haa+"'><span>"+a[0]+"排"+a[1]+"号</span><span class='price'>票档："+a[2]+"元</span></p>")
        $(".msg-3").append("<p class='"+hee+"'>￥"+a[2]+"</p>")
        $(".msg-4").append("<p class='"+hei+"'>1</p>")
        $(".msg-5").html("￥"+money)
    }
})

/**
 * 查询配送方式
 * @param commdityId
 */
function findPsType(commdityId,placeName,sessionTime){
    $.ajax({
        type: "POST",
        url:"http://cn.pn.com/api-pn/scheduler/commoditysession/p/getCommodityInfo",
        data:{commodityId:commdityId},
        dataType:"json",
        success:function (data) {
            var  psType=data.data.logistics;;
            var type=psType.split(",");
            for(var i=0;i<type.length;i++){
                var ha="";
                var la="";
                if(type[i]==1){
                    ha="快递配送";
                    la="sd";

                }
                if(type[i]==2){
                    ha="票牛自取";
                    la="sb";
                }
                if(type[i]==3){
                    ha="现场取票";
                    la="sa"
                }
                if(type[i]==4){
                    ha="电子票";
                    la="sc"
                }
                $(".bill").append("<p typeId='"+type[i]+"' style='display: inline-block;' id='"+la+"'>"+ha+"</p>")
                $(".image img").attr("src",data.data.imgUrl);
                $("#itemName").html(data.data.commodityName);
                $("#placeName").html(placeName);
                $("#sessionTime").html(sessionTime);
                $("#piao").html();

            }
        },
        error:function (data) {
            alert("kkkk");
        }
    })
}
//传入商品id
function findCoupon(commodityId){
    alert(commodityId);
    $.ajax({
        headers:{
            token: getCookie("token")
        },
        type: "post",
        url: "http://cn.pn.com/api-pn/user/Coupon/v/findCouponOrder",
        data:{commodityId:commodityId},
        dateType:"json",
        async:false,
        success: function (data) {
            $(".coupon").html("");
            $.each(data,function(i,e){
                var startTime=formatDate(new Date(e.startTime));
                var endTime=formatDate(new Date(e.endTime));
                $(".coupon").append("<div class=\"bottom layui-col-xs6 layui-col-sm6 layui-row-md6\">\n" +
                    "\t\t\t\t\t<div class=\"yhj\">\n" +
                    "\t\t\t\t\t\t<div class=\"yhj_l\">\n" +
                    "\t\t\t\t\t\t\t"+e.couponValue+"\n" +
                    "\t\t\t\t\t\t</div>\n" +
                    "\t\t\t\t\t\t<div class=\"yhj_r\">\n" +
                    "\t\t\t\t\t\t\t<div class=\"yhj_r_t\">\n" +
                    "\t\t\t\t\t\t\t\t<div class=\"d\">"+e.couponName+" </div>\n" +
                    "\t\t\t\t\t\t\t\t<div></div>\n" +
                    "\t\t\t\t\t\t\t</div>\n" +
                    "\t\t\t\t\t\t\t<div class=\"yhj_r_b layui-col-md8\">\n" +
                    "\t\t\t\t\t\t\t\t有效期："+startTime+" - "+endTime+"\n" +
                    "\t\t\t\t\t\t\t</div>\n" +
                    "\t\t\t\t\t\t\t<div  style=\"display: none\"></div>\n" +
                    "<input type=\"button\" value=\"使用\" class=\"layui-btn layui-btn-sm layui-btn-danger layui-col-md4 shiyong\">"+
                    "\t\t\t\t\t\t</div>\n" +
                    "\t\t\t\t\t</div>\n" +
                    "\t\t\t\t</div>")

            })
        }
    })
}
function formatDate(now) {
    var year=now.getFullYear();
    var month=now.getMonth()+1;
    var date=now.getDate();
    /*    var hour=now.getHours();
        var minute=now.getMinutes();
        var second=now.getSeconds();*/
    return year+"-"+month+"-"+date/*+" "+hour+":"+minute+":"+second*/;
}

function shiyong(){
    $(".shiyong").click(function () {
        yhj=$(this).parent().prev(".yhj_l").text().trim();
        $(".shiyong").attr({"disabled":true});
        $(".shiyong").css('background','#555');
        $(this).css('background','');
        $(this).attr({"disabled":false});
        if($(this).val()=="取消"){
            $(this).val("使用");
            $(".shiyong").attr({"disabled":false});
            $(".shiyong").css('background','');
        }else{
            $(this).val("取消");
        }
    })
}



function getRequest() {
    var url = window.location.search; //获取url中"?"符后的字串
    url=url.replace("%-%","?");
    url=url.replace("equals","=");
    var theRequest = new Object();
    if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        strs = str.split("&");
        for(var i = 0; i < strs.length; i ++) {
            //就是这句的问题
            theRequest[strs[i].split("=")[0]]=decodeURI(strs[i].split("=")[1]);
        }
    }
    return theRequest;
}

function charng(){
    $(".asd").css('border','#000');
    $(".asd").css('background-color','#fff');
    $(".asd").css('color','#555');
    $("#sa").css('border','1px solid #ffc2cb');
    $("#sa").css('background-color','#fff5f8');



    $(".bill").on("click","#sa",function () {
        deliverType=$(this).attr("typeId");
        $(".asd").css('border','#000');
        $(".asd").css('background-color','#fff');
        $(".asd").css('color','#555');
        $(this).css('border','1px solid #ffc2cb');
        $(this).css('background-color','#fff5f8');

        $(".tck").css('display','none')
        $("#ka").css('display','block')
    })

    $(".bill").on("click","#sb",function () {
        deliverType=$(this).attr("typeId");
        $(".asd").css('border','#000');
        $(".asd").css('background-color','#fff');
        $(".asd").css('color','#555');
        $(this).css('border','1px solid #ffc2cb');
        $(this).css('background-color','#fff5f8');

        $(".tck").css('display','none')
        $("#kb").css('display','block')
    })

    $(".bill").on("click","#sc",function () {
        deliverType=$(this).attr("typeId");
        $(".asd").css('border','#000');
        $(".asd").css('background-color','#fff');
        $(".asd").css('color','#555');
        $(this).css('border','1px solid #ffc2cb');
        $(this).css('background-color','#fff5f8');

        $(".tck").css('display','none')
        $("#kc").css('display','block')
    })

    $(".bill").on("click","#sd",function () {
        deliverType=$(this).attr("typeId");
        $(".asd").css('border','#000');
        $(".asd").css('background-color','#fff');
        $(".asd").css('color','#555');
        $(this).css('border','1px solid #ffc2cb');
        $(this).css('background-color','#fff5f8');

        $(".tck").css('display','none')
        $("#kd").css('display','block')
        findBytatus();
    })
}


function findBytatus(){
    $.ajax({
        headers:{
            token: getCookie("token")
        },
        type: "post",
        url: "http://cn.pn.com/api-pn/base/address/v/select",
        dateType:"json",
        async:false,
        success: function (data) {
            $("#addsss").html(" ")
            $.each(data.data,function(i,e){
                $("#addsss").append("              <div>\n" +
                    "                  <p class=\'fl\'><input type=\'radio\' name=\'adddd\' checked=\'checked\'></p>\n" +
                    "                  <p style=\'display:inline-block \'>"+e.receiveAddress+"("+e.receiveName+")收"+e.telephone+"</p>\n" +
                    "              </div>");
            })
        }
    })
}


